#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int m, n;
int dp[N];
int main() {
  cin >> m >> n;
  while (n--) {
    int v, w;
    cin >> v >> w;
    for (int j = m; j >= v; --j) {
      dp[j] = max(dp[j], dp[j - v] + w);
    }
  }
  cout << dp[m] << endl;
}
